//
// Created by Apple on 2021/8/9.
//

#include <unordered_map>
#include <string>
#include <utility>
#include "gmock/gmock.h"

using namespace testing;

class IniParser{
public:
    std::pair<std::string, std::string> parse(std::string& conf){
        std::size_t pos = conf.find("=");
        if(pos != std::string::npos){
            std::string key = conf.substr(0, pos);
            std::string value = conf.substr(pos+1);
            std::pair<std::string, std::string> conf(key, value);
            conf_.insert(conf);
            return conf;
        }
        return std::pair<std::string, std::string>();
    }

    std::string getValueBykey(std::string key){
        auto iter = conf_.find(key);
        if(iter != conf_.end()){
            return iter->second;
        }
        return "";
    }
private:
    std::unordered_map<std::string, std::string> conf_;
};

class IniParserTest : public Test{
public:
    IniParser parser;
};


TEST_F(IniParserTest, CheckParserReturnKey){
    std::string conf = "MaxSize=512";
    auto result = parser.parse(conf);
    ASSERT_THAT(result.first, Eq("MaxSize"));
}

TEST_F(IniParserTest, CheckParserReturnValue){
    std::string conf = "MaxSize=512";
    auto result = parser.parse(conf);
    ASSERT_THAT(result.second, Eq("512"));
}

TEST_F(IniParserTest, CheckGetValueBykey){
    std::string conf = "MaxSize=512";
    auto result = parser.parse(conf);
    auto key = parser.getValueBykey("MaxSize");
    ASSERT_THAT(key, Eq("512"));
}





